import jwt, { Secret } from 'jsonwebtoken';

// JWT密钥
const JWT_SECRET: Secret = process.env.JWT_SECRET || 'default_secret';

// 生成JWT token
export function generateToken(payload: object, expiresIn: string = '7d'): string {
  return jwt.sign(payload, JWT_SECRET, { expiresIn });
}

// 校验并解析JWT token
export function verifyToken(token: string): any {
  try {
    return jwt.verify(token, JWT_SECRET);
  } catch (err) {
    return null;
  }
}